package routers

import (
	. "gitee.com/edgebox/metadata/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func GetDevices(c *gin.Context){
	var devs Devices
	if err :=devs.Load();err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}else{
		if str,err:= devs.ToXml();err != nil{
			c.JSON(http.StatusInternalServerError,reply{
				Code:-1,
				Msg:err.Error(),
				Data:nil,
			})
			return
		}else{
			c.JSON(http.StatusOK,reply{
				Code:0,
				Msg:"success",
				Data:string(str),
			})
			return
		}
	}
}
func SetDevices(c *gin.Context){
	var devs Devices
	err :=c.BindXML(&devs)
	if err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}
	if err :=devs.Save();err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}else{
		c.JSON(http.StatusOK,reply{
			Code:0,
			Msg:"success",
			Data:nil,
		})
		return
	}
}
